<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    protected function showAjaxMessage($message,$success=false){
        echo CJSON::encode(array(
            'msg'=>$message,
            'success'=>$success
            
        ));
        Yii::app()->end();
    }
    public function beforeAction($action) {
        parent::beforeAction($action);

        $filterAction = array(
            'index', 'update', 'create',
        );
        if (!in_array($action->id, $filterAction)) {
            return true;
        }
        $currentCtrl = NULL;

        $currentCtrl = Yii::app()->controller->id;

        if (in_array(Yii::app()->controller->action->id, $filterAction)) {
            $currentAction = Yii::app()->controller->action->id;
            $currentParams = $_GET;
        } else {
            $currentAction = NULL;
            $currentParams = NULL;
        }
        $lastCtrl = Yii::app()->session['last_controller'];
        $lastAction = Yii::app()->session['last_action'];
        $lastParams = Yii::app()->session['last_params'];

        if (isset(Yii::app()->session['last_controller']))
            if (isset(Yii::app()->session['last_action']))
                if (isset(Yii::app()->session['last_params'])) {
                    
                }

        if ($currentCtrl != NULL) {
            Yii::app()->session['last_controller'] = $currentCtrl;
        }

        if ($currentAction != NULL)
            Yii::app()->session['last_action'] = $currentAction;
        if ($currentParams != NULL)
            Yii::app()->session['last_params'] = $currentParams;

/*
        if ($currentAction == 'update') {
            $flag = false;
            switch ($currentCtrl) {
                case 'acctcat':
                    $this->lock('tbl_acctcat', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'coa':
                    $this->lock('tbl_coa', 'account=:account', array('account' => $currentParams['account']));
                    break;
                case 'companyinfo':
                    $this->lock('tbl_companyinfo', 'name=:name', array('name' => $currentParams['name']));
                    break;
                case 'counters':
                    $this->lock('tbl_counters', 'countername=:countername', array('countername' => $currentParams['countername']));
                case 'customers':
                    $this->lock('tbl_customers', 'custno=:id', array('id' => $currentParams['id']));
                    break;
                case 'dispatchdepartments':
                    $this->lock('tbl_dispatchdepartments', 'name=:name', array('name' => $currentParams['name']));
                case 'dispatchdetails':
                    if (isset($currentParams['dispatch'])) {
                        $this->lock('tbl_dispatchdetails', 'dispatch=:dispatch', array('dispatch' => $currentParams['dispatch']));
                    }
                    break;
                case 'dispatchequipment':
                    if(isset($currentParams['id']))
                        $this->lock('tbl_dispatchequipment', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'dispatchtech':
                    $this->lock('tbl_dispatchtech', 'id=:id', array('id' => $currentParams['id']));
                case 'documents':
                    $this->lock('tbl_documents', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'employees':
                    $this->lock('tbl_employees', 'empno=:empno', array('empno' => $currentParams['empno']));
                    break;
                case 'employeecontacts':
                    $this->lock('tbl_employeecontacts', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'employeedocuments':
                    $this->lock('tbl_employeedocuments', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'employeeequipment':
                    $this->lock('tbl_employeeequipment', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'employeenotes':
                    $this->lock('tbl_employeenotes', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'equipment':
                    $this->lock('tbl_equipment', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'equipmenttype':
                    $this->lock('tbl_equipmenttype', 'equiptype=:equiptype', array('equiptype' => $currentParams['equiptype']));
                    break;
                case 'invcategories':
                    $this->lock('tbl_invcategories', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'inventory':
                    if (isset($currentParams['partno'])) {
                        $this->lock('tbl_inventory', 'partno=:partno', array('partno' => $currentParams['partno']));
                    }
                    break;
                case 'invsubcategories':
                    $this->lock('tbl_invsubcategories', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'locationcontacts':
                    $this->lock('tbl_locationcontacts', 'id=:id', array('id' => $currentParams['id']));
                    break;

                case 'locations':
                    if (isset($currentParams['id'])) {
                        $this->lock('tbl_locations', 'id=:id', array('id' => $currentParams['id']));
                    }
                    break;
                case 'markupcodes':
                    $this->lock('tbl_markupcodes', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'mfg':
                    $this->lock('tbl_mfg', 'mfg=:mfg', array('mfg' => $currentParams['mfg']));
                    break;
                case 'pages':
                    $this->lock('tbl_pages', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'plantypes':
                    $this->lock('tbl_plantypes', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'pricebooks':
                    $this->lock('tbl_pricebooks', 'book=:book', array('book' => $currentParams['book']));
                    break;
                case 'sickhrs':
                    $this->lock('tbl_sickhrs', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'statuscode':
                    $this->lock('tbl_statuscode', 'statuscode=:statuscode', array('statuscode' => $currentParams['id']));
                    break;
                case 'terms':
                    $this->lock('tbl_terms', 'termid=:termid', array('termid' => $currentParams['id']));
                    break;
                case 'timezone':
                    $this->lock('tbl_timezone', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'users':
                    $this->lock('tbl_users', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'vacationhrs':
                    $this->lock('tbl_vacationhrs', 'id=:id', array('id' => $currentParams['id']));
                    break;
                case 'vendorparts':
                    $tihs->lock('tbl_vendorparts', 'int=:int', array('int' => $currentParams[$id]));
            }
        }

        if (($currentCtrl == $lastCtrl && $lastAction != $currentAction && $lastAction == 'update') ||
                ($currentCtrl != $lastCtrl && $lastAction == 'update')) {
            switch ($lastCtrl) {
                case 'acctcat':
                    $this->unlock('tbl_acctcat', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'coa':
                    $this->unlock('tbl_coa', 'account=:account', array('account' => $lastParams['account']));
                    break;
                case 'companyinfo':
                    $this->unlock('tbl_companyinfo', 'name=:name', array('name' => $lastParams['name']));
                    break;
                case 'counters':
                    $this->unlock('tbl_counters', 'countername=:countername', array('countername' => $lastParams['countername']));
                case 'customers':
                    $this->unlock('tbl_customers', 'custno=:id', array('id' => $lastParams['id']));
                    break;
                case 'dispatchdepartments':
                    $this->unlock('tbl_dispatchdepartments', 'name=:name', array('name' => $lastParams['name']));
                case 'dispatchdetails':
                    if (isset($lastParams['dispatch'])) {
                        $this->unlock('tbl_dispatchdetails', 'dispatch=:dispatch', array('dispatch' => $lastParams['dispatch']));
                    }
                    break;
                case 'dispatchequipment':
                    if(isset($currentParams['id']))
                        $this->unlock('tbl_dispatchequipment', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'dispatchtech':
                    $this->unlock('tbl_dispatchtech', 'id=:id', array('id' => $lastParams['id']));
                case 'documents':
                    $this->unlock('tbl_documents', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'employees':
                    $this->unlock('tbl_employees', 'empno=:empno', array('empno' => $lastParams['empno']));
                    break;
                case 'employeecontacts':
                    $this->unlock('tbl_employeecontacts', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'employeedocuments':
                    $this->unlock('tbl_employeedocuments', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'employeeequipment':
                    $this->unlock('tbl_employeeequipment', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'employeenotes':
                    $this->unlock('tbl_employeenotes', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'equipment':
                    $this->unlock('tbl_equipment', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'equipmenttype':
                    $this->unlock('tbl_equipmenttype', 'equiptype=:equiptype', array('equiptype' => $lastParams['equiptype']));
                    break;
                case 'invcategories':
                    $this->unlock('tbl_invcategories', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'inventory':
                    if (isset($lastParams['partno'])) {
                        $this->unlock('tbl_inventory', 'partno=:partno', array('partno' => $lastParams['partno']));
                    }
                    break;
                case 'invsubcategories':
                    $this->unlock('tbl_invsubcategories', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'locationcontacts':
                    $this->unlock('tbl_locationcontacts', 'id=:id', array('id' => $lastParams['id']));
                    break;

                case 'locations':
                    if (isset($lastParams['id'])) {
                        $this->unlock('tbl_locations', 'id=:id', array('id' => $lastParams['id']));
                    }
                    break;
                case 'markupcodes':
                    $this->unlock('tbl_markupcodes', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'mfg':
                    $this->unlock('tbl_mfg', 'mfg=:mfg', array('mfg' => $lastParams['mfg']));
                    break;
                case 'pages':
                    $this->unlock('tbl_pages', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'plantypes':
                    $this->unlock('tbl_plantypes', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'pricebooks':
                    $this->unlock('tbl_pricebooks', 'book=:book', array('book' => $lastParams['book']));
                    break;
                case 'sickhrs':
                    $this->unlock('tbl_sickhrs', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'statuscode':
                    $this->unlock('tbl_statuscode', 'statuscode=:statuscode', array('statuscode' => $lastParams['id']));
                    break;
                case 'terms':
                    $this->unlock('tbl_terms', 'termid=:termid', array('termid' => $lastParams['id']));
                    break;
                case 'timezone':
                    $this->unlock('tbl_timezone', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'users':
                    $this->unlock('tbl_users', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'vacationhrs':
                    $this->unlock('tbl_vacationhrs', 'id=:id', array('id' => $lastParams['id']));
                    break;
                case 'vendorparts':
                    $tihs->unlock('tbl_vendorparts', 'int=:int', array('int' => $lastParams[$id]));
                    break;
            }
        
        }
 * *
 */
        return true;
    }

    public function lock($table, $condition, $params) {
        $lock = Yii::app()->db->createCommand('select lockby,locktime from ' . $table . ' where ' . $condition)->bindValues($params)->queryRow();
        $flag=false;
        if ($lock == false || $lock['lockby'] == NULL) {
            $flag = true;
        } else if ($lock['locktime'] != NULL) {
            $diff = strtotime(time()) - strtotime($lock['locktime']);
            if ($diff > 60 * 60 * 30) {
                $flag = true;
            }
        }
        if ($flag) {
            Yii::app()->db->createCommand()->update($table, array(
                'lockby' => Yii::app()->user->name,
                'locktime' => date('Y-m-d H:i:s')
                    ), $condition, $params);
        }
    }

    public function unlock($table, $condition, $params) {

        $lastLock = Yii::app()->db->createCommand('select lockby from ' . $table . ' where ' . $condition)->bindValues($params)->queryScalar();

        if ($lastLock != false && $lastLock != NULL && $lastLock == Yii::app()->user->name) {

            Yii::app()->db->createCommand()->update($table, array(
                'lockby' => "",
                'locktime' => ''
                    ), $condition, $params);
        }
    }

}
